var Oitsm = {
	version : "1.0"
};

Oitsm.loadUtil = {
	
		/**
		 * loaded js map
		 * key : js path
		 * value : load flag (true or false)
		 */
		loadedFile : {
			
		},
		/**
		 * loaded css
		 * key : js path
		 * value : load flag (true or false)
		 */
		loadedCss : {
			
		},
		
		// 解析 js
		decode : window.execScript?window.execScript:window.eval,
		// 解析 css		
		appendCss : function(css){
			var el = document.createElement("style");
			el.setAttribute("type", "text/css");
			if(el.styleSheet){
				el.styleSheet.cssText = css;
			}else{
				el.appendChild(document.createTextNode(css));
			}
			document.getElementByTagName("head")[0].appendChild(el);
		},
		
		loadCss : function(url,callback,failure){
			if(this.loadedCss[url]){
				return;
			}
			Ext.Ajax.request({
				url : webContext + url,
				success : function(response){
					this.appendCss(response.responseText);
					callback();
				},
				failure : function(){
					failure();
				},
				scope : this
			});
			this.loadedCss[url] = true;
		},
		/**
		 * load js file
		 * path --> js paths , split by ","
		 * callBack --> invoke after loaded,  callBack(isSuccess)
		 */
		load : function(paths,callBack,scope,enableCache){
			var refresh = false;
			if(enableCache!=undefined){
				refresh = !enableCache;
			}else if(window.jsDebug != undefined){
				refresh = window.jsDebug;
			}
			var pathArr = paths.split(',');
			var sum = pathArr.length;
			var successCount = 0;
			var count = 0;
			var logs = [];
			var responseCache = {};
			for(i=0;i<sum;i++){
				responseCache[i] = '';
				var path = pathArr[i];
				if(path == '' || (this.loadedFile[path] && !refresh)){
					successCount ++;
					count++;
					Oitsm.loadUtil.afterLoad(callBack,scope,sum,successCount,count,logs,responseCache,pathArr);
					continue;
				}	
				Oitsm.loadUtil.doGet(webContext + path,refresh,function(){
					Oitsm.loadUtil.loadedFile[path] = true;
					successCount ++;
					count++;
					Oitsm.loadUtil.afterLoad(callBack,scope,sum,successCount,count,logs,responseCache,pathArr);
				},function(){
					Oitsm.loadUtil.loadedFile[path] = false;
					count++;
					Oitsm.loadUtil.afterLoad(callBack,scope,sum,successCount,count,logs,responseCache,pathArr);
				},logs,responseCache,i);
			}
		},
		/**
		 * do after a js loaded
		 */
		afterLoad : function(callBack,scope,sum,successCount,count,logs,responseCache,pathArr){
			if(count >= sum){
				for(j=0;j<sum;j++){
					try{
						if(responseCache[j]){
							var path = pathArr[j];
							if(path && path.substring(path.length-4)=='css'){
								Oitsm.loadUtil.appendCss(responseCache[j]);
							}else{
								window.execScript?window.execScript(responseCache[j]):window.eval(responseCache[j]);
							}
						}
					}catch(e){
						logs.push('解析文件时发生错误:'+pathArr[j] + ',错误描述:'+e);
					}
				}
				
				if(callBack!=undefined){
					callBack.apply(scope?scope:{},[successCount==sum,logs]);
				}
			}
		},
		/**
		 * do a get request
		 */
		doGet : function(url,refresh,successFn,failureFn,logs,responseCache,index){
			Ext.Ajax.request({
				method : refresh?'POST':'GET',
				url : url,
				success : function(response,option){
					responseCache[index] = response.responseText;
					successFn();
				},
				failure : function(response,option){
					logs.push('下载文件时发生错误,错误代码:'+response.status+',url:'+url);
					failureFn();
				}
			});
		}
};
Oitsm.JSUtil = {
	downloadIframe : null,
	getJsonStore : function(url, fields) {
		var store = new Ext.data.JsonStore({
			id : Ext.id(),
			url : url,
			root : "data",
			fields : fields,
			totalProperty : "rowCount",
			remoteSort : false,
			timeout : 8000
		});
		return store;
	},
	
	failureHandler : function(response,opts){
		if(response.status == 404){
			Ext.Msg.alert("提示","请求服务器失败,错误原因:404,请求地址不存在或服务器已关闭!");
		}else if(response.status == 500){
			Ext.Msg.alert("提示","请求服务器失败,错误原因:500,服务器发生异常!");
		}else{
			Ext.Msg.alert("提示","请求失败,请联系管理员解决!");
		}
	},
	
	getCloseBtn : function(fn){
		var btn = new Ext.Button({
			text : '关闭',
			iconCls : 'close',
			handler : fn
		});
		return btn;
	},
	
	getSaveBtn : function(fn){
		var btn = new Ext.Button({
			text : '保存',
			iconCls : 'save',
			handler : fn
		});
		return btn;
	},
	
	getSearchBtn : function(fn){
		var btn = new Ext.Button({
			text : '查询',
			iconCls : 'search',
			handler : fn
		});
		return btn;
	},
	
	doInvoke : function(panelId,methodName,params){
		var cmp = Ext.getCmp(panelId);
		try{
			cmp[methodName](params);
		}catch(e){}
	},
	
	download : function(url){
		if(this.downloadIframe == null){
			this.downloadIframe = document.createElement("iframe");
			this.downloadIframe.src = url;
			this.downloadIframe.style.display = 'none';
			document.appendChild(this.downloadIframe);
		}else{
			this.downloadIframe.src = url;
		}
	}
};
Oitsm.initer = {
	doInit : function(){
		this.intI18N();
		this.intVType();
		this.initPlugIn();
		this.loadJS();
		this.override();
	},
	
	override : function(){
	},
	
	intVType : function(){
		Ext.apply(Ext.form.VTypes, {
		    IPAddress:  function(v) {
		        return /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(v);
		    },
		    IPAddressText: 'IP地址不合法',
		    IPAddressMask: /[\d\.]/i
		});
	},
	
	intI18N : function(){
	},
	
	initPlugIn : function(){
	},
	
	loadJS : function(){
	}
};